import BigNumber                         from 'bignumber.js';

/**
 * @description 将科学计数法转变成正常用户可视格式
 * @param num 需要转换的数值
 */
export function toNonExponential(num) {
    if (null == num || !num) return ''
    if (isNaN(Number(num))) return num
    if (typeof num === 'string') num = Number(num)
    var m = num.toExponential().match(/\d(?:\.(\d*))?e([+-]\d+)/) || []
    return num.toFixed(Math.max(0, (m[1] || '').length - m[2]))
}

/**
 * 比对大小排序
 * @param p  object对象中要比对的key值
 * @param type  1 从大到小   0 从小到大
 */
export const compare = (p,type) => {
    return function(m,n){
        let a = m[p];
        let b = n[p];
        return type === 1?b-a:a-b;
    }
}

/**
 * @description 加法
 * @param num1 加数1
 * @param num2 加数2
 */
export const accAdd = (num1, num2) => {
    if(!num1) num1 = 0;
    if(!num2) num2 = 0;
    return Number(new BigNumber(num1).plus(num2))
}

/**
 * @description 减法
 * @param num1 减数
 * @param num2 被减数
 */
export const subtr = (num1,num2) => {
    return Number(new BigNumber(num1).minus(num2))
}

/**
 * @description 乘法
 * @param num1 乘数1
 * @param num2 乘数2
 */
export const accMul = (num1,num2) => {
    return Number(new BigNumber(num1).multipliedBy(num2))
}

export function cheng(num1=0,num2=0) {
    if(isNaN(Number(num1))) num1 = 0;
    if(isNaN(Number(num2))) num2 = 0;
    if(typeof num1 === 'string') num1 = Number(num1);
    if(typeof num2 === 'string') num2 = Number(num2);
    return Number(new BigNumber(num1).multipliedBy(num2))
}

/**
 * @description 除法
 * @param num1 除数
 * @param num2 被除数
 */
export const accDiv = (num1,num2) => {
    return Number(new BigNumber(num1).dividedBy(num2))
}

/**
 * 截取小数位
 * @param target 被截取对象
 * @param num    需要截取的小数位
 */
export const countNum = (target,num) => {
    if(isNaN(Number(target))) return target
    if(!num) return parseInt(target)
    if(typeof target === 'number') target = target.toString();
    if(target && target.indexOf('.') > -1){
        let arrNewVal = target.split('.');
        //若不足切割小数位，则强制保留对应位数
        if(arrNewVal[1].length <= num){
            return Number(target).toFixed(num)
        }
        if(arrNewVal.length > 0 && arrNewVal[1].length > num){
            arrNewVal[1] = arrNewVal[1].substr(0,num);
            target = arrNewVal.join('.');
        }else{
            return Number(target).toFixed(num)
        }
    }else{
        return Number(target).toFixed(num)
    }
    return target
}

//时间格式化
export function formatDate(temp){
    if(!temp) return ''
    temp = temp.toString().length==10?temp*1000:temp;
    var _time = new Date(temp);
    var H = _time.getHours();
    var M = _time.getMinutes();
    var S = _time.getSeconds();
    H = H.toString().length===1?'0'+H:H;
    M = M.toString().length===1?'0'+M:M;
    S = S.toString().length===1?'0'+S:S;
    return H+':'+M+':'+S
}

/**
 * @description 转换时间格式
 */
export const transTime3 = temp => {
    //确保时间戳为13位
    temp = temp.toString().length == 10 ? temp * 1000 : temp;
    temp = Number(temp);
    let _newDate = new Date(temp);
    let postYear = _newDate.getFullYear();
    let postMonth = _newDate.getMonth() + 1;
    let postDate = _newDate.getDate();
    let postHours = _newDate.getHours();
    let postMinutes = _newDate.getMinutes();
    let postMM = _newDate.getSeconds();
    postMonth = postMonth.toString().length === 1 ? '0' + postMonth : postMonth;
    postDate = postDate.toString().length === 1 ? '0' + postDate : postDate;
    postHours = postHours.toString().length === 1 ? '0' + postHours : postHours;
    postMinutes = postMinutes.toString().length === 1 ? '0' + postMinutes : postMinutes;
    postMM = postMM.toString().length === 1 ? '0' + postMM : postMM;
    return postMonth + '/' + postDate + ' ' + postHours + ':' + postMinutes
}
